package cn.wolfcode.shop.web.controller;

import cn.wolfcode.shop.domain.User;
import cn.wolfcode.shop.redis.key.UserKeyPrefix;
import cn.wolfcode.shop.service.IUserService;
import cn.wolfcode.shop.web.util.CookieUtil;
import cn.wolfcode.shop.web.vo.LoginVo;
import cn.wolfcode.shop.result.CodeMsg;
import cn.wolfcode.shop.result.Result;
import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CookieValue;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

/**
 * Created by wolfcode-lanxw
 */
@Controller
public class LoginController {
    @Reference
    private IUserService userService;
    @RequestMapping("/do_login")
    @ResponseBody
    public Result<String> doLogin(@Valid LoginVo loginVo, HttpServletResponse response){
        String token = userService.login(loginVo.getMobile(), loginVo.getPassword());
        CookieUtil.addToCookie(response, UserKeyPrefix.TOKEN_NAME,token,UserKeyPrefix.USER_TOKEN.getExpireSeconds());
        return Result.success(token);
    }
}
